1
/****************************** Module Header ******************************\
2 Module Name: WinINet.cs
3 Project: CSWebBrowserWithProxy
4 Copyright (c) Microsoft Corporation.
6 This class is used to set the proxy. or restore to the system proxy for the
9 This source is subject to the Microsoft Public License.
10 See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 All other rights reserved.
13 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 \***************************************************************************/
19 using System
.Runtime
.InteropServices
;
20 using System
.Diagnostics
;
22 namespace CSWebBrowserWithProxy
24 public static class WinINet
26 static string agent
= Process
.GetCurrentProcess().ProcessName
;
29 /// Set the LAN connection proxy server for current process.
31 /// <param name="proxyServer">
34 /// <returns></returns>
35 public static bool SetConnectionProxy(bool isMachineSetting
, string proxyServer
)
39 return SetConnectionProxy(null, proxyServer
);
43 return SetConnectionProxy(agent
, proxyServer
);
48 /// Set the LAN connection proxy server.
50 /// <param name="agentName">
51 /// If agentName is null or empty, this function will set the Lan proxy for
52 /// the machine, else for the current process.
54 /// <param name="proxyServer">The Proxy Server.</param>
55 /// <returns></returns>
56 public static bool SetConnectionProxy(string agentName
, string proxyServer
)
58 IntPtr hInternet
= IntPtr
.Zero
;
61 if (!string.IsNullOrEmpty(agentName
))
63 hInternet
= NativeMethods
.InternetOpen(
65 (int)INTERNET_OPEN_TYPE
.INTERNET_OPEN_TYPE_DIRECT
,
71 return SetConnectionProxyInternal(hInternet
, proxyServer
);
75 if (hInternet
!= IntPtr
.Zero
)
77 NativeMethods
.InternetCloseHandle(hInternet
);
83 /// Set the proxy server for LAN connection.
85 static bool SetConnectionProxyInternal(IntPtr hInternet
, string proxyServer
)
89 INTERNET_PER_CONN_OPTION
[] Options
= new INTERNET_PER_CONN_OPTION
[3];
92 Options
[0] = new INTERNET_PER_CONN_OPTION();
93 Options
[0].dwOption
= (int)INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_FLAGS
;
94 Options
[0].Value
.dwValue
= (int)INTERNET_OPTION_PER_CONN_FLAGS
.PROXY_TYPE_PROXY
;
97 Options
[1] = new INTERNET_PER_CONN_OPTION();
99 (int)INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_SERVER
;
100 Options
[1].Value
.pszValue
= Marshal
.StringToHGlobalAnsi(proxyServer
);
103 Options
[2] = new INTERNET_PER_CONN_OPTION();
104 Options
[2].dwOption
=
105 (int)INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_BYPASS
;
106 Options
[2].Value
.pszValue
= Marshal
.StringToHGlobalAnsi("local");
108 // Allocate a block of memory of the options.
109 System
.IntPtr buffer
= Marshal
.AllocCoTaskMem(Marshal
.SizeOf(Options
[0])
110 + Marshal
.SizeOf(Options
[1]) + Marshal
.SizeOf(Options
[2]));
112 System
.IntPtr current
= buffer
;
114 // Marshal data from a managed object to an unmanaged block of memory.
115 for (int i
= 0; i
< Options
.Length
; i
++)
117 Marshal
.StructureToPtr(Options
[i
], current
, false);
118 current
= (System
.IntPtr
)((int)current
+ Marshal
.SizeOf(Options
[i
]));
121 // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
122 INTERNET_PER_CONN_OPTION_LIST option_list
= new INTERNET_PER_CONN_OPTION_LIST();
124 // Point to the allocated memory.
125 option_list
.pOptions
= buffer
;
127 // Return the unmanaged size of an object in bytes.
128 option_list
.Size
= Marshal
.SizeOf(option_list
);
130 // IntPtr.Zero means LAN connection.
131 option_list
.Connection
= IntPtr
.Zero
;
133 option_list
.OptionCount
= Options
.Length
;
134 option_list
.OptionError
= 0;
135 int size
= Marshal
.SizeOf(option_list
);
137 // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance.
138 IntPtr intptrStruct
= Marshal
.AllocCoTaskMem(size
);
140 // Marshal data from a managed object to an unmanaged block of memory.
141 Marshal
.StructureToPtr(option_list
, intptrStruct
, true);
143 // Set internet settings.
144 bool bReturn
= NativeMethods
.InternetSetOption(
146 INTERNET_OPTION
.INTERNET_OPTION_PER_CONNECTION_OPTION
,
149 // Free the allocated memory.
150 Marshal
.FreeCoTaskMem(buffer
);
151 Marshal
.FreeCoTaskMem(intptrStruct
);
153 // Throw an exception if this operation failed.
156 throw new ApplicationException(" Set Internet Option Failed!");
159 // Notify the system that the registry settings have been changed and cause
160 // the proxy data to be reread from the registry for a handle.
161 NativeMethods
.InternetSetOption(
163 INTERNET_OPTION
.INTERNET_OPTION_SETTINGS_CHANGED
,
166 NativeMethods
.InternetSetOption(
168 INTERNET_OPTION
.INTERNET_OPTION_REFRESH
,
175 /// Get the current system options for LAN connection.
176 /// Make sure free the memory after restoration.
178 public static INTERNET_PER_CONN_OPTION_LIST
GetSystemProxy()
181 // Query following options.
182 INTERNET_PER_CONN_OPTION
[] Options
= new INTERNET_PER_CONN_OPTION
[3];
184 Options
[0] = new INTERNET_PER_CONN_OPTION();
185 Options
[0].dwOption
= (int)INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_FLAGS
;
186 Options
[1] = new INTERNET_PER_CONN_OPTION();
187 Options
[1].dwOption
= (int)INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_SERVER
;
188 Options
[2] = new INTERNET_PER_CONN_OPTION();
189 Options
[2].dwOption
= (int)INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_BYPASS
;
191 // Allocate a block of memory of the options.
192 System
.IntPtr buffer
= Marshal
.AllocCoTaskMem(Marshal
.SizeOf(Options
[0])
193 + Marshal
.SizeOf(Options
[1]) + Marshal
.SizeOf(Options
[2]));
195 System
.IntPtr current
= (System
.IntPtr
)buffer
;
197 // Marshal data from a managed object to an unmanaged block of memory.
198 for (int i
= 0; i
< Options
.Length
; i
++)
200 Marshal
.StructureToPtr(Options
[i
], current
, false);
201 current
= (System
.IntPtr
)((int)current
+ Marshal
.SizeOf(Options
[i
]));
204 // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
205 INTERNET_PER_CONN_OPTION_LIST Request
= new INTERNET_PER_CONN_OPTION_LIST();
207 // Point to the allocated memory.
208 Request
.pOptions
= buffer
;
210 Request
.Size
= Marshal
.SizeOf(Request
);
212 // IntPtr.Zero means LAN connection.
213 Request
.Connection
= IntPtr
.Zero
;
215 Request
.OptionCount
= Options
.Length
;
216 Request
.OptionError
= 0;
217 int size
= Marshal
.SizeOf(Request
);
219 // Query system internet options.
220 bool result
= NativeMethods
.InternetQueryOption(
222 INTERNET_OPTION
.INTERNET_OPTION_PER_CONNECTION_OPTION
,
228 throw new ApplicationException("Get System Internet Option Failed! ");
235 /// Restore to the system proxy settings.
237 public static bool RestoreSystemProxy()
239 return RestoreSystemProxy(agent
);
243 /// Restore to the system proxy settings.
245 public static bool RestoreSystemProxy(string agentName
)
247 if (string.IsNullOrEmpty(agentName
))
249 throw new ArgumentNullException("Agent name cannot be null or empty!");
252 IntPtr hInternet
= IntPtr
.Zero
;
255 if (!string.IsNullOrEmpty(agentName
))
257 hInternet
= NativeMethods
.InternetOpen(
259 (int)INTERNET_OPEN_TYPE
.INTERNET_OPEN_TYPE_DIRECT
,
265 return RestoreSystemProxyInternal(hInternet
);
269 if (hInternet
!= IntPtr
.Zero
)
271 NativeMethods
.InternetCloseHandle(hInternet
);
277 /// Restore to the system proxy settings.
279 static bool RestoreSystemProxyInternal(IntPtr hInternet
)
281 var request
= GetSystemProxy();
283 int size
= Marshal
.SizeOf(request
);
286 IntPtr intptrStruct
= Marshal
.AllocCoTaskMem(size
);
288 // Convert structure to IntPtr
289 Marshal
.StructureToPtr(request
, intptrStruct
, true);
291 // Set internet options.
292 bool bReturn
= NativeMethods
.InternetSetOption(
294 INTERNET_OPTION
.INTERNET_OPTION_PER_CONNECTION_OPTION
,
298 // Free the allocated memory.
299 Marshal
.FreeCoTaskMem(request
.pOptions
);
300 Marshal
.FreeCoTaskMem(intptrStruct
);
304 throw new ApplicationException(" Set Internet Option Failed! ");
307 // Notify the system that the registry settings have been changed and cause
308 // the proxy data to be reread from the registry for a handle.
309 NativeMethods
.InternetSetOption(
311 INTERNET_OPTION
.INTERNET_OPTION_SETTINGS_CHANGED
,
315 NativeMethods
.InternetSetOption(
317 INTERNET_OPTION
.INTERNET_OPTION_REFRESH
,